package com.hy.myapp.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hy.myapp.Entity.User;
import com.hy.myapp.mapper.UserMapper;
import com.hy.myapp.util.CryptUtil;

@Service
public class UserService {
	private final static Logger logger = LoggerFactory.getLogger(UserService.class);
	
	@Autowired
	private UserMapper userMapper;
	
	public boolean isUniqueName(String uname) {
		logger.info("Will check username:{}'s uniqueness",uname);
		User found=userMapper.findByName(uname);
		return found==null;
	}
	
	public boolean isUniqueEmail(String email) {
		logger.info("Will check email:{}'s uniqueness",email);
		User found=userMapper.findByEmail(email);
		return found==null;
	}
	
	public boolean addOneUser2Db(String uname,String pswd,String email) {
		logger.info("Will add one user name={},email={}",uname,email);
		String enctyptPswd=CryptUtil.getMD5Digest(pswd);
		return (userMapper.addUser(uname, enctyptPswd, email)==1);
	}
	
	public User searchUserByNamePswd(String uname,String pswd) {
		logger.info("Will search user name={}",uname);
		String enctyptPswd=CryptUtil.getMD5Digest(pswd);
		User found=userMapper.findByNamePswd(uname, enctyptPswd);
		
		return found;
	}
}
